using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace TripleMatch
{
    public partial class DecorationsManager : MonoBehaviour
    {
        private static DecorationsManager s;
        public static DecorationsManager S
        {
            get
            {
                return s;
            }
        }

        private void Awake()
        {
            s = this;
            InitDecorationsData();
        }

        private void OnDestroy()
        {
            SaveDecorationsData();
        }

        public void OnMatch(int index)
        {
            bool containedData = false;
            for (int i = 0; i < m_decoractionSaveData.decorationPiecesUnSettledDatas.Count; i++)
            {
                if (m_decoractionSaveData.decorationPiecesUnSettledDatas[i].id == index)
                {
                    m_decoractionSaveData.decorationPiecesUnSettledDatas[i].pieseCount++;
                    containedData = true;
                    break;
                }
            }

            if (!containedData)
            {
                DecorationPiecesData decorationPiecesData = new DecorationPiecesData();
                decorationPiecesData.id = index;
                decorationPiecesData.pieseCount = 1;
                m_decoractionSaveData.decorationPiecesUnSettledDatas.Add(decorationPiecesData);
            }
        }

        /// <summary>
        /// 游戏结束/开始的时候进行碎片结算
        /// 结算流程中先展示本轮获得的碎片数量，对于玩家首次获得的物品直接按照比例兑换，展示获取动画，玩家可以在商店页用获得的碎片兑换对应的物品
        /// </summary>
        public void OnSettlement(out List<DecorationPiecesData> unsettleDatas, out List<int> firstGainDecoration)
        {
            unsettleDatas = new List<DecorationPiecesData>();
            firstGainDecoration = new List<int>();

            //没有未结算的碎片时，结算流程直接结束
            if (m_decoractionSaveData.decorationPiecesUnSettledDatas.Count <= 0)  return;

            //结算的数据处理
            for (int i = 0; i < m_decoractionSaveData.decorationPiecesUnSettledDatas.Count; i++)
            {
                bool hasReachTranRate = AddDecorationPicesData(m_decoractionSaveData.decorationPiecesUnSettledDatas[i].id, m_decoractionSaveData.decorationPiecesUnSettledDatas[i].pieseCount);
                if(hasReachTranRate) firstGainDecoration.Add(m_decoractionSaveData.decorationPiecesUnSettledDatas[i].id);
            }

            unsettleDatas = m_decoractionSaveData.decorationPiecesUnSettledDatas.ToList();
            m_decoractionSaveData.decorationPiecesUnSettledDatas.Clear();
        }


        public void OnMove(int decorationMixId, int layer, Vector2 postion)
        { 
           
        }
    }
}

